﻿using Helper;
using Helper.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Starehe.ViewModels
{
    public class MainWindowVM : ViewModelBase    
    {
        ViewModelBase source;
        ObservableCollection<ViewModelBase> backJournal;
        bool navigatingBack;
        public MainWindowVM() 
        {
            InitVars();
            CreateCommands();            
        }

        public ViewModelBase Source
        {
            get { return this.source; }

            set
            {
                if (value != this.source)
                {                   
                    this.source = value;                    
                    NotifyPropertyChanged("Source");
                    if (!navigatingBack)
                        BackJournal.Add(source);
                }
            }
        }
        
        public ObservableCollection<ViewModelBase> BackJournal
        { get { return backJournal; } }

        protected override void InitVars()
        {
            navigatingBack = false;
            backJournal = new ObservableCollection<ViewModelBase>();
            Source = new HomePageVM();   
        }

        protected override void CreateCommands()
        {
            DisplayStudentDetailCommand = new RelayCommand(o =>
            {
                this.Source = new StudentDetailsVM((StudentModel)o);
            }, o => (o is StudentModel));

            DisplayStaffDetailCommand = new RelayCommand(o =>
            {
                this.Source = new StaffDetailsVM((StaffModel)o);
            }, o => (o is StudentModel));
            
            DisplayStudentsCommand = new RelayCommand(o =>
            {
                this.Source = new StudentsVM();
            }, o => true);

            DisplayStaffCommand = new RelayCommand(o =>
            {
                this.Source = new StaffVM();
            }, o => true);

            DisplayFeesCommand = new RelayCommand(o =>
            {
                this.Source = new FeesVM();
            }, o => true);

            DisplaySchoolCommand = new RelayCommand(o =>
            {
                this.Source = new SchoolVM();
            }, o => true);

            DisplayTimeTableCommand = new RelayCommand(o =>
            {
                this.Source = new TimeTableVM();
            }, o => true);

            DisplayExamsCommand = new RelayCommand(o =>
            {
                this.Source = new ExamsVM();
            }, o => true);

            DisplayEventsCommand = new RelayCommand(o =>
            {
                this.Source = new EventsVM();
            }, o => true);

            DisplayGalleryCommand = new RelayCommand(o =>
            {
                this.Source = new GalleryVM();
            }, o => true);
                        
            DisplaySettingsCommand = new RelayCommand(o =>
            {
                this.Source = new SettingsVM();
            }, o => true);
            
            BackCommand = new RelayCommand(o =>
            {
                navigatingBack = true;
                int index = backJournal.Count - 2;
                backJournal.RemoveAt(index+1);
                this.Source = backJournal[index];
                navigatingBack = false;
            }, o => CanGoBack());
        }

        private bool CanGoBack()
        {
            if (backJournal.Count < 1)
                return false;
            else return true;
        }

        public ICommand DisplayStudentDetailCommand
        {
            get;
            private set;
        }

        public ICommand DisplayStaffDetailCommand
        {
            get;
            private set;
        }

        public ICommand DisplayStudentsCommand
        {
            get;
            private set;
        }

        public ICommand DisplayStaffCommand
        {
            get;
            private set;
        }

        public ICommand DisplayFeesCommand
        {
            get;
            private set;
        }

        public ICommand DisplaySchoolCommand
        {
            get;
            private set;
        }

        public ICommand DisplayTimeTableCommand
        {
            get;
            private set;
        }

        public ICommand DisplayExamsCommand
        {
            get;
            private set;
        }
                
        public ICommand DisplayEventsCommand
        {
            get;
            private set;
        }

        public ICommand DisplayGalleryCommand
        {
            get;
            private set;
        }

        public ICommand DisplaySettingsCommand
        {
            get;
            private set;
        }

        public ICommand BackCommand
        {
            get;
            private set;
        }

        public override void Reset()
        {
            
        }

    }
}
